package keepsoft.lcw.main;

import java.util.List;
import java.util.Map;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import keepsoft.lcw.service.IntegratingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


public class Main {
	public static final Logger logger = LoggerFactory.getLogger(Main.class); 

	public static Integer YEAR=2018;
	public static Integer MONTH=1;
	public static Integer DAY=24;

	public static void main(String[] args){
		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"});
		IntegratingService integratingService = (IntegratingService) context.getBean("integratingService");
		schedule(integratingService);
	}

	private static void schedule(IntegratingService integratingService) {
		int corePoolSize = 1;//线程池个数
		int initialDelay = 0;//任务第一次启动的时间（单位：s/秒）
		int period = 24;//任务间隔时间(单位：s/秒)

		ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(corePoolSize);
		scheduledExecutorService.scheduleAtFixedRate(new TimerTask() {
			@Override
			public void run() {
				logger.info("start");
				long startTime = System.currentTimeMillis();
				insertData(integratingService);
				long endTime = System.currentTimeMillis();
				logger.info("insert is run ：" + (endTime - startTime) + "ms"); //795375ms
			}
		}, initialDelay, period, TimeUnit.HOURS);
	}

	/***
	 * 模拟插入数据
	 */
	public static void insertData(IntegratingService integratingService){
		integratingService.insertData();
	}

	/**
	 * 查询所有设备
	 */
	public static List findMonitors(IntegratingService integratingService){
		List list=integratingService.findMonitors();
		return list;
	}

}
